$(function(){
	
	var childId = 0;

	//获取当前时间
	function getNowFormatDate() {
	    var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	            + " " + date.getHours() + seperator2 + date.getMinutes();
	            // + seperator2 + date.getSeconds();
	    return currentdate;
	}
	//加载--顾问--信息
	function requestCounselorsS(data){
		if( data.status == true ){
			$(".saler-list").html("");
			var str = "";
			for( var i = 0; i< data.data.content.length; i++ ){
				var item = data.data.content[i];
				str += '<option  class = ""'+ 'value ="'+item.id+'">'+item.name+'</option>';

			}
			
			$(".saler-list").html(str);
		}
	}
	function requestCounselorsE(data){
		alert("加载顾问失败+Error");
	}
	//加载--潜在会员--信息请求成功
	function requestSuccess(data){
		if( data.status == true ){
			if( data.data.content.length == 0 ){
				alert("无记录");
				return;
			}
			totalPages = data.data.totalPages;
			if( data.data.totalPages ){
				$("#page-total").html(data.data.totalPages);
			}else{
				$("#page-total").html(1);
			}
			$("#page-num").html(pageNum);

			var node2 = $(".detail-check").find("tr").eq(0).clone();
			$(".detail-check").html("");

			for( var i = 0; i< data.data.content.length; i++ ){
				var item = data.data.content[i];
				var node = node2.clone();
				
				node.find(".replace-id").html(item.id);
				node.find(".replace-name").html(item.name);
				node.find(".replace-gender").html(item.gender);
				node.find(".replace-age").html(item.age);
				node.find(".replace-birth").html(item.birth);
				node.find(".replace-linkman").html(item.linkman);
				node.find(".replace-phone").html(item.phone);	
				node.find(".replace-follower").html(item.follower);
				node.find(".replace-points").html(item.points);
				node.find(".replace-createdTime").html(item.createdTime);
				node.find(".replace-followTime").html(item.followTime);		
				node.removeClass("hide");

				$(".detail-check").append(node);
				console.log(i);
			}
		}else{
			alert("获取记录失败");
		}
	}

	//加载--潜在会员--信息请求失败
	function requestError(data){
		alert("获取潜在会员失败 error");
	}

	//添加--潜在会员--请求成功
	function regSuccess(data){
		if( data.status == true ){
			alert("添加成功");
			location.reload();
		}else{
			alert(data.msg+"添加失败");
		}
	}
	//添加--潜在会员--请求失败
	function regError(data){
		alert("添加失败 error");
	}
	//搜索--潜在会员--请求成功
	function findSuccess(data){
		if( data.status ){
	  	var node2 = $(".detail-check").find("tr").eq(0).clone();
	  	$(".detail-check").html("");
	  	// for( var i = 0; i< data.data.content.length; i++ ){
				var item = data.data.content[0];
				var node = node2.clone();
				
				node.find(".replace-id").html(item.id);
				node.find(".replace-name").html(item.name);
				node.find(".replace-gender").html(item.gender);
				node.find(".replace-age").html(item.age);
				node.find(".replace-birth").html(item.birth);
				node.find(".replace-linkman").html(item.linkman);
				node.find(".replace-phone").html(item.phone);	
				node.find(".replace-follower").html(item.follower);
				node.find(".replace-points").html(item.points);
				node.find(".replace-createdTime").html(item.createdTime);
				node.find(".replace-followTime").html(item.followTime);		
				node.removeClass("hide");
				
				node.removeClass("hide");
				$(".detail-check").append(node);
			// }
		}
	}

	//搜索--失败
	function findError(data){
		console.log(data.msg);
	}

	//删除--请求成功
	function deleteSuccess(data,obj){
		if( data.status == true ){
			alert("删除成功!");
			location.reload();
		}else{
			alert("删除失败");
		}
	}
	//删除--请求成功
	function deleteError(data){
		alert("删除失败Error");
	}
	//修改--信息信息获取
	function setInfoSuccess(data){
		console.log(data);
		if( data.status == true ){
			alert("修改成功");
			location.reload();
		}else{
			alert("修改失败");	
		}
	}
	function setInfoError(data){
		alert("修改失败Error");		
	}

	//详情--获取学员信息成功
	function getInfoSuccess(data){
		if( data.status == true ){
			$(".post-id2").html(data.data.content[0].id);
			$(".post-name2").html(data.data.content[0].name);
			// $(".saler-psw2").html();
			$(".post-gender2").html(data.data.content[0].gender);
			$(".post-age2").html(data.data.content[0].age);
			$(".post-linkman2").html(data.data.content[0].linkman);
			$(".post-phone2").html(data.data.content[0].phone);
			$(".post-createdTime2").html(data.data.content[0].createdTime);
			$(".post-followTime2").html(data.data.content[0].followTime);
			$(".post-point2").html(data.data.content[0].point);
			$('.saler-list option:selected').html(data.data.content[0].follower),
			$('.saler-list option:selected').val(data.data.content[0].counselorId),
			$(".table-box").addClass("hide");
			$(".account-box").removeClass("hide");
			$(".table-box2").removeClass("hide");
		}
	}
	//详情--获取学员信息失败
	function getInfoError(data){
			if( data.status == false ){
				alert("获取失败");
			}
	}
	//--获取跟进记录
	function getRecordSuccess(data){

		var node2 = $(".follow-record").find("tr").eq(0).clone();
		$(".follow-record").html("");
		for( var i = 0; i < data.data.content.length;i++ ){
			var node = node2.clone();
			node.find(".recordTime").html(data.data.content[i].time);
		    node.find(".recordDes").html(data.data.content[i].description);
		    node.removeClass("hide");
		    $(".follow-record").append(node);
		}
	    
	}
	function getRecordError(data){
		alert("获取跟进记录失败Error");
	}

	//--搜索学员成功
	function searchSuccess(data){
		if( data.status == true ){
			$(".detail-check").html("");
		}
	}
	//添加跟进记录
	function addConfirmSuccess(data){
		var node = $(".follow-record").find("tr").eq(0).clone();
		node.find(".recordTime").html(data.data.time);
		node.find(".recordDes").html(data.data.description);
		node.removeClass("hide");
		$(".follow-record").append(node);
		$(".close-btn").click();
	}
	function addConfirmError(data){
		alert("添加记录失败Error");
	}

	//--点击列表查看信息
	$(".detail-check").on("click",".child-record",function(){
		// console.log("click");
		add= 0;
		cancel = true;
		console.log($(this).find("th:first").text());
		$(this).click(function(){
			 childId = $(this).find("th:first").text();
			 $.ajax({
				type: "get",
				url: 'http://106.75.143.27/students',
				// data: {"counselorsId":counselorsId},
				data: {"page":0,"id":childId},
				contentType: 'application/x-www-form-urlencoded',
				success: getInfoSuccess,
				error: getInfoError
			});
			 $.ajax({
				type: "get",
				url: 'http://106.75.143.27/followRecords',
				// data: {"counselorsId":counselorsId},
				data: {"page":0,"studentId":childId},
				contentType: 'application/x-www-form-urlencoded',
				success: getRecordSuccess,
				error: getRecordError
			});
			
		});
	});

	//--添加学员跟踪记录
	$('#myModal').on('shown.bs.modal', function () {
	  $('#myInput').focus();
	});

	//--修改学员信息
	$(".info-modify").click(function(){
		$(".post-info").each(function(){
			// $(this).attr("contenteditable", "true");
			$(this).attr("contenteditable", "true");
			$(this).addClass("compile-box");
		})
		$(".add-record").addClass("hide");
		$(".book-class").addClass("hide");
		$(".info-modify").addClass("hide");
		$(".info-back").addClass("hide");
		$(".modify-confirm").removeClass("hide");
		$(".modify-back").removeClass("hide");
	});

	//--确认添加/修改潜在学员信息
	$(".modify-confirm").click(function(){
		if(add){
			console.log("add");
			add = 0;
			var data = {
				"name": $(".post-name2").html(),
				"gender": $(".post-gender2").html(),
				"age": $(".post-age2").html(),
				"birth": $(".post-birth2").html(),
				"linkman": $(".post-linkman2").html(),
				"phone": $(".post-phone2").html(),
				"follower": $('.saler-list option:selected').html(),
				"counselorId":$('.saler-list option:selected').val(),
				"point": $(".post-post-point2").html(),
				"createdTime": $(".post-createdTime2").html(),
				"followTime":$(".post-followTime2").html(),
				"remainingClass":0
			};
			console.log(data);
			
			Ajax({
				url: "http://106.75.143.27/students",
				type: "post",
				data: JSON.stringify(data),
				contentType: 'application/json',
				dataType: 'json',
				call1:  regSuccess,
				call2:  regError
			});
		}else{
			add = 0;
			console.log("modify");
			var data = {
				"name": $(".post-name2").html(),
				"gender": $(".post-gender2").html(),
				"age": $(".post-age2").html(),
				"birth": $(".post-birth2").html(),
				"linkman": $(".post-linkman2").html(),
				"phone": $(".post-phone2").html(),
				"points": $(".post-point").html(),
				"entryTime": $(".post-createTime2").html(),
				"followTime":$(".post-followTime2").html(),
				"follower": $('.saler-list option:selected').html(),
				"counselorId":$('.saler-list option:selected').val(),
			};
			console.log(data);
			$.ajax({
				type: "put",
				url: "http://106.75.143.27/students/"+childId,
				data: JSON.stringify(data),
				contentType: 'application/json',
				dataType: 'json',
				success: setInfoSuccess,
				error: setInfoError
			});
		}
	});

	//--取消修改学员信息
	$(".modify-back").click(function(){
		$(".post-info").each(function(){
			$(this).attr("contenteditable", "false");
			$(this).removeClass("compile-box");
		});
		// $(".teacher-info").attr("contenteditable", "false").removeClass("compile-box");
		if( add ){//取消添加
			add = 0;
			$(".table-box1").removeClass("hide");
			$(".account-box").addClass("hide");
			$(".table-box2").addClass("hide");

			$(".add-record").addClass("hide");
			$(".book-class").removeClass("hide");
			$(".info-modify").removeClass("hide");
			$(".info-back").removeClass("hide");
			$(".saler-confirm").addClass("hide");
			$(".add-back").addClass("hide");
			return;
		}else{//取消修改
			$(".add-record").removeClass("hide");
			$(".info-modify").removeClass("hide");
			$(".info-back").removeClass("hide");
			$(".book-class").removeClass("hide");
			$(".modify-confirm").addClass("hide");
			$(".modify-back").addClass("hide");
		}
	})

	//--返回学员记录页面
	$(".info-back").click(function(){
		$(".table-box1").removeClass("hide");
		$(".account-box").addClass("hide");
		$(".table-box2").addClass("hide");
	});

	//--保存跟进信息
	$(".add-confirm").click(function(){
		var text = $(".add-record-text").html();
		var data =  {"studentId":childId,"description":text,"time":getNowFormatDate()};
		console.log(data);
		Ajax({
	        url: 'http://106.75.143.27/followRecords',
	        type:"post",
	        data: JSON.stringify(data),
	        call1: addConfirmSuccess,
	        call2: addConfirmError
	    });
	})

	//--搜索学员
	$("#select-child").click(function(){
		var searchText = $("#search-number").val();
		console.log(searchText);
		$.ajax({
			type: "get",
			url: 'http://106.75.143.27/students',
			data: {"page":0,"name":searchText},
			contentType: 'application/x-www-form-urlencoded',
			success: findSuccess,
			error: findError
		});
	});
	//--删除潜在会员
	$(".detail-check").on("click","#delete-info",function(){
		 childId = $(this).parent().find("th:first").text();
		 console.log(childId);
		 $.ajax({
			type: "delete",
			url: 'http://106.75.143.27/students/'+childId,
			// contentType: 'application/x-www-form-urlencoded',
			success: deleteSuccess,
			error: deleteError
		});		
		 return false;
		
	});

	//--添加学员
	$("#add-child").click(function(){
		add = 1;
		$(".table-box1").addClass("hide");
		$(".account-box").removeClass("hide");
		$(".post-info").each(function(){
			// $(this).attr("contenteditable", "true");
			$(this).html("");
			$(this).attr("contenteditable", "true");
			$(this).addClass("compile-box");
		});
		$(".post-info").html("").attr("contenteditable", "true").addClass("compile-box");
		$(".add-record").addClass("hide");
		$(".book-class").addClass("hide");
		$(".info-modify").addClass("hide");
		$(".info-back").addClass("hide");
		$(".modify-confirm").removeClass("hide");
		$(".modify-back").removeClass("hide");
	});

	//前一页
	$("#page-pre").click(function(){
		 pageNum = $("#page-num").html();
		 
		if( pageNum == 1 ){
			alert("已经是第1页了哦");
		}else{
			 pageNum--;
			 var num = pageNum -1;
			 console.log(num);
			$.ajax({
				type: "get",
				url: 'http://106.75.143.27/students?sort=createdTime,desc',
				data: {"page":num},
				contentType: 'application/x-www-form-urlencoded',
				success: requestSuccess,
				error: requestError
			});
		}
	});
	//--后一页
	$("#page-next").click(function(){
		
		 pageNum = $("#page-num").html();
		
		if( pageNum >= (totalPages) ){
			alert("已经是最后一页了哦");
		}else{
			pageNum++;
			var num = pageNum-1;
			console.log(num);
			$.ajax({
				type: "get",
				url: 'http://106.75.143.27/students?sort=createdTime,desc',
				data: {"page":num},
				contentType: 'application/x-www-form-urlencoded',
				success: requestSuccess,
				error: requestError
			});
		}
	});
	//--跳转页面
	$("#skip").click(function(){
		var num = $("#js-skip").val();
		console.log(num);
		if( ( num < 1 )||( num > totalPages ) ){
			alert("请输入正确页数");
		}else{
			$.ajax({
				type: "get",
				url: 'http://106.75.143.27/students?sort=createdTime,desc',
				data: {"page":num-1},
				contentType: 'application/x-www-form-urlencoded',
				success: requestSuccess,
				error: requestError
			});
		}
	});
	

	//加载顾问信息
	$.ajax({
		type: "get",
		url: 'http://106.75.143.27/counselors?sort=createdTime,desc',
		data: {"page":0},
		contentType: 'application/x-www-form-urlencoded',
		success: requestCounselorsS,
		error: requestCounselorsE
	});
	//加载潜在会员记录
	$.ajax({
		type: "get",
		url: 'http://106.75.143.27/students',
		data: {"page":0},
		contentType: 'application/x-www-form-urlencoded',
		success: requestSuccess,
		error: requestError
	});

	
})